/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.gwt.widgets;

import java.util.ArrayList;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.DOM;

public class HoverTable extends FlexTable {
	private String highLightStyle;
	private String normalStyle;
	private int highLightIdx;
	
	private ArrayList tableListeners = new ArrayList();
	
	public HoverTable(String normalStyle, String highLightStyle) {
        super();

        sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONMOUSEOVER | Event.ONMOUSEOUT);         
        this.highLightStyle = highLightStyle;
        this.normalStyle = normalStyle;
        highLightIdx = -1;
	}

	public void addTableListener(TableListener l) {
		tableListeners.add(l);
	}
	
	public void removeTableListener(TableListener l) {
		tableListeners.remove(l);
	}
	
    private Element getMouseEventTargetCell(Event event) {
        Element td = DOM.eventGetTarget(event);
        //locate enclosing td element
        while (!DOM.getAttribute(td, "tagName").equalsIgnoreCase("td")) {
            // If we run out of elements, or run into the table itself, then give up.
            if ((td == null) || DOM.compare(td, getElement()))
                return null;
            td = DOM.getParent(td);
        }
        //test if the td is actually from this table
        Element tr = DOM.getParent(td);
        Element body = DOM.getParent(tr);
        if (DOM.compare(body, this.getBodyElement())) {
            return td;
        }
        //Didn't find appropriate cell
        return null;         
    }

    //gets the row index of a given table cell
    protected int getRowIndex(Element cell) {
		Element row = DOM.getParent(cell);
		Element table = DOM.getParent(row);

		return DOM.getChildIndex(table, row);
    }
    
    protected void onMouseEnterRow(int idx) {
    	highLight(idx);
    }
    
    protected void onMouseLeaveRow(int idx) {
    	unHighLight(idx);
    }
    
	public void onBrowserEvent(Event e) {
		Element cell;
		switch(DOM.eventGetType(e)) {
			case Event.ONMOUSEOVER:
				cell = getMouseEventTargetCell(e);
				
				
				if(cell == null) return;

//				DOM.setStyleAttribute(cell, "backgroundColor", "");
				
				onMouseEnterRow(getRowIndex(cell));
				
				break;
			case Event.ONMOUSEOUT:

				cell = getMouseEventTargetCell(e);

				if(cell == null) return;
				
				onMouseLeaveRow(getRowIndex(cell));
				
				break;
				
            case Event.ONMOUSEDOWN:
            	cell = getMouseEventTargetCell(e);
				if(cell == null) return;
				onMouseLeaveRow(getRowIndex(cell));
            	
				for(int i = 0; i < tableListeners.size(); i++) {
					((TableListener)tableListeners.get(i)).onCellClicked(this, getRowIndex(cell), 0);
				}
				
                break;
        } 				
		
	}

	public void highLight(int idx) {
		System.out.println("h: " + idx);
		if(idx > -1 && idx < getRowCount()) {
			this.getRowFormatter().setStyleName(idx, highLightStyle);
		}
		
		if(highLightIdx > -1 && highLightIdx != idx) {
			System.out.println("u:" + highLightIdx);
			unHighLight(highLightIdx);	
		}
		
		highLightIdx = idx;
	}
	
	public void unHighLight(int idx) {
		if(idx > -1 && idx < getRowCount()) {
			this.getRowFormatter().setStyleName(idx, normalStyle);			
		}
		if(idx == highLightIdx)
			highLightIdx = -1;
	}
}
